package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     发送验证
     方式一
     前端
     axios.post("/user/sendMsg？phone=" + this.form.phone)
     @GetMapping("/sendMsg")
     @RequestParam("phone") String phone
     */

    /**
     * 方式二
     * 前端
     * axios.post("/user/sendMsg", {"phone":this.form.phone})
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        String code = userService.sendMsg(user,session);
        return R.success(code,"短信发送成功了");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        String phone = (String)map.get("phone");
        String code = (String)map.get("code");
        User user = userService.login(phone,code,session);
        return R.success(user);
    }

    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session){
        return userService.loginout(session);
    }
}
